package org.oa.ai.chatController;

import dev.langchain4j.service.TokenStream;
import org.oa.ai.config.AssistantUnique;
import org.oa.ai.config.YcsqAssistant;
import org.oa.ai.config.YyAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.LocalDate;

@RestController
@RequestMapping("/ycsq")
@CrossOrigin
public class YysqChatController {

    @Autowired
    YcsqAssistant ycsqAssistant;  // 带着存储消息并且消息隔离

    /**
     * 带着存储使用流式方式返回数据并区分用户隔离
     */
    @CrossOrigin
    @GetMapping(value = "/sendMessage", produces = MediaType.TEXT_EVENT_STREAM_VALUE)  // 设置返回给前端文本的编码
    public Flux<String> assistantUniqueStream(@RequestParam(defaultValue="你好！") String message,
                                              @RequestParam(value = "userId") String userId,
                                              @RequestParam(value = "deptId") String deptId,
                                              @RequestParam(value = "userName") String userName,
                                              @RequestParam(value = "deptName") String deptName
    ) {
        TokenStream stream = ycsqAssistant.stream(userId, message, LocalDate.now().toString(), userId, userName, deptId, deptName);
        Flux<String> objectFlux = Flux.create(sink -> {
            stream.onPartialResponse(s -> sink.next(s))
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();
        });
        return objectFlux
                .concatWith(Flux.just("[complete]"));
    }

}
